<template>
  <div class="container">
    <div class="install_top flex">
      <el-select v-model="username" placeholder="请选择归属" popper-class='sect' filterable @change="infoChange" style="margin-right:20px" size="mini">
        <el-option v-for="(item,index) in affiliation" :key="index" :label="`${item.username} - ${item.title}`" :value="item.id"></el-option>
      </el-select>
      <el-select v-model="listQuery.bId" placeholder="请选择批次" @change="selectBatch" popper-class='sect' size="mini">
        <el-option v-for="(item,index) in batchArr" :key="index" :label="item.batch" :value="item.id"></el-option>
      </el-select>
      <el-select v-model="listQuery.packageName" placeholder="请选择应用" @change="onClickApp" popper-class='sect' size="mini" style="margin-left:20px">
        <el-option v-for="(item,index) in appArr" :key="index" :label="item.app_name" :value="item.bg"></el-option>
      </el-select>
      <div class="appArr_txt">( 合计应用: {{appArr.length}} )</div>
    </div>
    <div class="appCharts flex flex_cen flex-wrap">
      <div class="charts_box">
        <div class="unload_box flex felx_align">
          <div class="txt" :class="appIndex != -1 ? appArr[appIndex].app_forceinstall ? 'noCoerce' : 'coerce' : ''">
            {{appIndex != -1 ? appArr[appIndex].app_forceinstall ? '非强制安装应用' : '强制安装应用' : ''}}
          </div>
          <div class="txt" :class="appIndex != -1 ? appArr[appIndex].app_attribution ? 'noPublic' : 'public' : ''" style="margin-left:10px">
            {{appIndex != -1 ? appArr[appIndex].app_attribution ? '专属应用' : '公共应用' : ''}}
          </div>
        </div>
        <appchart :height="'350px'" :btDat="statisticsData" />
        <div class="details_txt" v-show="appNumber.installTotal">应用部署状态:<span class="text">{{appNumber.installTotal}}(已安装)</span> / {{appNumber.snTotal}}台 (设备总数)</div>
      </div>
    </div>
    <!-- 分类管理table -->
    <div style="margin-top:20px">
      <div class="table_search">
        <el-input placeholder="请输入sn" v-model="listQuery.sn" class="input-with-select" size="small">
          <el-button slot="append" icon="el-icon-search" @click="getinstallAppSnStatistics"></el-button>
        </el-input>
      </div>
      <el-table stripe :data="brandTable" style="width: 100%" :key="key" border fit highlight-current-row ref="tableDataRef">
        <el-table-column v-if="colData[0].istrue" prop="username" label="归属" min-width="80" align="center"></el-table-column>
        <el-table-column v-if="colData[1].istrue" prop="batch" label="批次" min-width="80" align="center"></el-table-column>
        <el-table-column v-if="colData[2].istrue" prop="app_name" label="应用名称" min-width="120" align="center"></el-table-column>
        <el-table-column v-if="colData[3].istrue" prop="app_version_name" label="版本号" min-width="80" align="center"></el-table-column>
        <el-table-column v-if="colData[4].istrue" prop="sn" label="SN码" min-width="120" align="center"></el-table-column>
        <el-table-column v-if="colData[5].istrue" prop="install_time" label="安装时间" min-width="140" align="center">
          <template slot-scope="scope">{{scope.row.install_time| newDate}}</template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="paging">
      <el-pagination :current-page.sync="listQuery.startSize" :page-sizes="[10, 50, 100, 200]" :page-size="listQuery.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" background
        @size-change="handleSizeChange" @current-change="handleCurrentChange"></el-pagination>
    </div>
  </div>
</template>

<script>
import { getBatchTwo, getAscription } from "@/api/equipment.js";
import { queryBatchApp, installAppSnStatistics } from "@/api/bordereaux.js";
import objectable from "@/utils/objectable.js";
import appchart from '../components/appChart'
export default {
  components: { appchart },
  data() {
    return {
      batchArr: [],
      appArr: [],
      statisticsData: [],
      name: '',
      affiliation: [],
      username: '',
      listQuery: { startSize: 1, pageSize: 10, userId: localStorage.getItem("ID"), packageName: undefined, bId: undefined, sn: undefined, type: 1 },
      checkboxVal: ["归属", "批次", "应用名称", "版本号", "SN码", "安装时间"],
      key: 1,
      colData: [], //动态控制table
      brandTable: [],
      total: 0, //总数量
      appNumber: {}
    }
  },
  created() {
    this.colData = objectable(this.checkboxVal, this.checkboxVal); //转为数组对象，好控制table
    this.getAscriptions()
  },
  computed: {
    appIndex() {
      return this.appArr.map(item => item.bg).indexOf(this.listQuery.packageName)
    }
  },
  methods: {
    infoChange(val) {
      this.listQuery.bId = ''
      this.getBatchTwo(val)
    },
    //设备维护获取归属用户信息
    getAscriptions() {
      getAscription({ userId: localStorage.getItem("ID") }).then(res => {
        this.affiliation = res.data.sort((x, y) => {
          let reg = /[a-zA-Z0-9]/
          if (reg.test(x.username) || reg.test(y.username)) {
            if (x.username > y.username) {
              return 1
            } else if (x.username < y.username) {
              return -1
            } else {
              return 0
            }
          } else {
            return x.username.localeCompare(y.username)
          }
        })
      }).catch(err => {
        this.affiliation = []
      })
    },
    getBatchTwo(id) {
      getBatchTwo({ userId: id }).then(res => {
        this.batchArr = res.data
      }).catch(err => {
        this.batchArr = []
      })
    },
    //下拉框选中事件
    selectBatch(val) {
      let params = { userId: localStorage.getItem("ID"), bId: val }
      queryBatchApp(params).then(res => {
        this.appArr = res.data
        //选中下拉框默认展示第一条数据
        this.listQuery.packageName = res.data[0].bg
        this.getinstallAppSnStatistics()
        this.name = res.data[0].app_name
      }).catch(err => {
        err.code == 400 ? this.$message.error(err.msg) : ''
        this.appArr = []
        this.statisticsData = []
        this.listQuery.packageName = ''
      })
    },
    //选中app名字
    onClickApp(value) {
      this.name = this.appArr.filter(item => {
        return item.bg.indexOf(value) > -1
      })[0].app_name
      this.listQuery.sn = undefined
      this.getinstallAppSnStatistics()
    },
    //应用安装统计数据
    getinstallAppSnStatistics(name) {
      installAppSnStatistics(this.listQuery).then(res => {
        this.brandTable = res.data.app
        this.statisticsData = [{ name: `${this.name} : 已安装`, value: res.data.installTotal },{name: '设备总数', value: res.data.snTotal}]
        this.appNumber = { installTotal: res.data.installTotal, snTotal: res.data.snTotal }
        this.total = res.total
      }).catch(err => {
        this.total = 0
        this.brandTable = []
        this.statisticsData = []
        this.appNumber = {}
      })
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val
      this.getinstallAppSnStatistics();
    },
    handleCurrentChange(val) {
      this.listQuery.startSize = val
      this.getinstallAppSnStatistics();
    },
    //下拉动态table子组件
    alterCheckbok(key, conte) {
      this.key = key;
      this.colData.filter(i => {
        if (conte.indexOf(i.title) != -1) {
          i.istrue = false;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        } else {
          i.istrue = true;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        }
      });
    }
  }
}
</script>

<style scoped lang="scss">
.container {
  margin: 0 !important;
  font-family: 微软雅黑, MicrosoftYahei, sans-serif;
  color: #fff;
  background-color: #ffffff;
  background-size: 100% 100%;
  background-position: 0 0;
  background-repeat: no-repeat;
  width: 100%;
  min-height: 100vh;
  padding: 20px;
  .button-paimay {
    color: #fff !important;
    background-color: #409eff !important;
    border-color: #409eff !important;
  }
  .info {
    color: #fff !important;
    background-color: #35454e !important;
    border-color: #35454e !important;
  }
  .app {
    margin-left: 20px;
    ul {
      li {
        background: #ecf5ff;
        border-color: #b3d8ff;
        display: inline-block;
        line-height: 1;
        white-space: nowrap;
        cursor: pointer;
        background: #fff;
        border: 1px solid #dcdfe6;
        color: #606266;
        -webkit-appearance: none;
        text-align: center;
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
        outline: 0;
        margin: 0;
        -webkit-transition: 0.1s;
        transition: 0.1s;
        font-weight: 500;
        padding: 12px 20px;
        font-size: 14px;
        border-radius: 4px;
        margin: 0 15px 20px 15px;
      }
    }
  }
  .table_search{
    width: fit-content;
    margin-bottom: 10px;
  }
}
.appCharts {
  margin-top: 20px;
  .charts_box {
    box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);
    border-radius: 5px;
    background-clip: padding-box;
    border: 1px solid #ebeef5;
    width: 60%;
    position: relative;
  }
}
.appArr_txt {
  color: #999;
  line-height: 28px;
  margin-left: 10px;
  font-size: 14px;
}
.unload_box {
  margin: 10px 0 0 10px;
  .txt {
    padding: 7px;
    border-radius: 4px;
    font-size: 12px;
  }
  .coerce {
    background: #409eff;
  }
  .noCoerce {
    background: #909399;
    margin: 0 10px;
  }
  .public {
    background: #e6a23c;
  }
  .noPublic {
    background: #67c23a;
  }
}
.details_txt {
  color: #999;
  font-size: 12px;
  position: absolute;
  bottom: 40px;
  left: 50%;
  transform: translateX(-50%);
  .text {
    color: #409eff;
  }
}
</style>

<style scoped>
/* /deep/ .el-input__inner {
  background: #409eff;
  color: #fff;
  border: 1px solid #409eff;
}
/deep/ .el-select .el-input .el-select__caret {
  color: #fff;
}
/deep/ .el-input__inner::-webkit-input-placeholder {
  color: #fff;
} */
</style>